/*
	Get all favorite. Add/Remove book item
*/
var favoriteVM = function () {

	// Variables for Favorite page
	var datacontext = new DataContext(),
		folders = ko.observableArray([]),
		list = ko.observableArray([]),
        topPopupSelect = ko.observable(100),
        heightPopupSelect = ko.observable(300),

		// Get all folders. Not show message when error
		getFolders = function (isServer) {
			list([]);			
			return datacontext.favoriteFolder.getAll({
				results: list,
                isServer:isServer,
				key: 'saveFilefavorite_folders',
                callbackFunc: function(){
                    if (list().length > 0){
                        var foldersEntity = list()[0].object;
                        folders(foldersEntity());
                                                     calculatorPupupSize();
                    }
                    stopLoading();
                }
			}).done(function () {
                CheckExpireSession(
                    list,
                    false,
                    function() {
                        getFolders();
                    },
                    function(entity) {
                        folders(entity());
                                   calculatorPupupSize();
                    }
                );
				
			}).fail(function (e) {
                    CheckExpireSessionErrorCallback(e,
                                                    function(){
                                                    /* session expired */
                                                    getFolders();
                                                    },
                                                    function(){
                                                    /* not expire */
                                                    });
			});
		},
        calculatorPupupSize = function(){
            var heightOfPopup = 47*folders().length + 40;
            var maxHeightOfPopup = $(window).height() - 200;
            if (heightOfPopup > maxHeightOfPopup){
                heightPopupSelect(maxHeightOfPopup);
                topPopupSelect(100);
            }
            else{
                heightPopupSelect(heightOfPopup);
                topPopupSelect((maxHeightOfPopup-heightPopupSelect())/2 + 100);
            }
            
        };
		
	return {
		getFolders: getFolders,
		folders: folders,
        heightPopupSelect: heightPopupSelect,
        topPopupSelect: topPopupSelect
	};
}()
